package com.intelligentmeterreadingsys.settingsys.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.intelligentmeterreadingsys.MainActivity;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.GridView.GridViewDao;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

/**
 * 基础设置主界面
 * 
 * @author king
 * 
 */
public class SettingSysActivity extends Activity implements OnClickListener {

    public Button button1;// web设置

    public Button button2;// GPS设置

    public Button button3;// 系统设置

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.settingsys);
	Verification();

    }

    /**
     * 初始化
     */
    public void inti() {
	button1 = (Button) findViewById(R.id.setting_web);
	button2 = (Button) findViewById(R.id.setting_gps);
	button3 = (Button) findViewById(R.id.setting_sys);
	button1.setOnClickListener(this);
	button2.setOnClickListener(this);
	button3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.setting_web:
	    Intent intent = new Intent(SettingSysActivity.this, SettingWebActivity.class);
	    startActivity(intent);

	    break;
	case R.id.setting_gps:
	    GridViewDao dao = new GridViewDao(this);
	    dao.queryNavigation("0127000361", "1", null, null);
	    dao.close();
	    Intent intent1 = new Intent(SettingSysActivity.this, SettingGpsActivity.class);
	    // Intent intent1=new Intent(SettingSysActivity.this,
	    // OperateExcelActivity.class);
	    startActivity(intent1);
	    break;
	case R.id.setting_sys:
	    Intent intent2 = new Intent(SettingSysActivity.this, SettingBasicsActivity.class);
	    startActivity(intent2);
	    break;
	default:
	    break;
	}

    }

    /**
     * 密码验证
     */
    public void Verification() {
	SharedPreferenceUtil sp = new SharedPreferenceUtil(this);
	final String pass = sp.getValue(XmlNameHelper.passWord);

	if (pass == "") {
	    inti();
	    return;
	}
	AlertDialog.Builder dialog = new AlertDialog.Builder(this);

	final EditText edit = new EditText(this);
	edit.setRawInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
	edit.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
	dialog.setIcon(android.R.drawable.ic_dialog_info);

	dialog.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		if (edit.getEditableText().toString().equals(pass)) {
		    dialog.dismiss();
		    inti();
		} else {
		    Toast.makeText(SettingSysActivity.this, "密码错误", Toast.LENGTH_LONG).show();
		    dialog.dismiss();
		    Intent intent = new Intent(SettingSysActivity.this, MainActivity.class);
		    startActivity(intent);
		}

	    }
	});
	dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		dialog.dismiss();
		Intent intent = new Intent(SettingSysActivity.this, MainActivity.class);
		startActivity(intent);

	    }
	});
	dialog.setMessage("密码验证");

	dialog.setView(edit);

	dialog.show();

    }

}
